package com.guchenbo.demo.jdbc;

import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author guchenbo
 * @Date 2020/4/10.
 */
public class Test {

    private ReentrantLock lock;
    private int[] arr;
    private AtomicInteger idx;

    public Test() {
        this.lock = new ReentrantLock(true);
        arr = new int[] { 1, 2, 3, 4, 5 };
        idx = new AtomicInteger();
    }

    public void print() {

        while (!Thread.interrupted()) {
            String name = Thread.currentThread().getName();
            int id = Integer.valueOf(name);
            int cnt = idx.get();
            System.out.println("cnt = " + cnt);

            if (cnt >= arr.length) {
                break;
            }
            int mod = cnt % 3;
            if (mod == id) {
                System.out.println("Thread: " + name + " print " + arr[cnt]);
                idx.incrementAndGet();
            }

            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }

    }

    public static void main(String[] args) {
//        ExecutorService service = Executors.newCachedThreadPool();
//        Test test = new Test();
//
//        for (int i = 0; i < 3; i++) {
//            Thread t = new Thread(() -> {
//                test.print();
//            }, String.valueOf(i));
//            //            service.execute(t);
//            t.start();
//        }
        int[] nums={9,100};
//        nums=null;
        Test t=new Test();
        int[] r = t.m(nums);
        System.out.println(Arrays.toString(r));
    }

    public  int[] m(int[] nums){
        if(nums==null||nums.length==0){
            return nums;
        }
        Map<Integer,Integer> map = new HashMap<>();
        int max=0;
        for (int i = 0; i < nums.length; i++) {
            int n=nums[i];
            map.put(n,map.getOrDefault(n,0)+1);
            if(n>max){
                max=n;
            }
        }
        int len=Math.max(nums.length,max);
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < len; i++) {
            int a=i+1;
            int cnt=map.getOrDefault(a,0);
            if(cnt>1){
                //chongfu
                list.add(a);
            }else if(cnt==0){
                //缺失
                list.add(a);
            }
        }
        int[] ret=new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            ret[i]=list.get(i);
        }
        return ret;
    }

}
